package qmlt.dataset;

import java.util.ArrayList;
import java.util.List;

/**
 * The instance, including dataset and id information, as well as features and target.
 * 
 * Note that NULL pointer in features indicating a missing value.
 * 
 * @author quyin
 */
public class Instance
{
	public final String		id;

	public final DataSet	dataSet;

	private List<Object>	features	= new ArrayList<Object>();

	private Object				target;

	public Instance(String id, DataSet dataSet)
	{
		this.id = id;
		this.dataSet = dataSet;
	}

	public List<Object> getFeatures()
	{
		return features;
	}
	
	public void setFeatures(List<Object> features)
	{
		this.features = features;
	}

	public Object getTarget()
	{
		return target;
	}

	public void setTarget(Object target)
	{
		this.target = target;
	}

	@Override
	public String toString()
	{
		return String.format("%s.%s:(%s)->%s", dataSet.getId(), id, features, target);
	}

	/**
	 * In some filters, we need to clone an instance to a new dataset, in order not to interfere with
	 * the original instance and dataset. However, this clone is shallow: values are copied by
	 * reference. If any operation is needed on the value, just create a new object to replace the
	 * original one.
	 * 
	 * Note that this method will also add the cloned instance to the new dataset automatically.
	 * 
	 * @param newDataSet
	 * @return
	 */
	public Instance shallowClone(DataSet newDataSet)
	{
		Instance inst = new Instance(id, newDataSet);
		for (Object obj : getFeatures())
			inst.getFeatures().add(obj);
		inst.setTarget(target);
		newDataSet.getInstances().add(inst);
		return inst;
	}

}
